<?php

class MarkerController extends Zend_Controller_Action 
{
    protected $_model;

    public function indexAction()
    {
        $model = $this->_getModel();
        $this->view->entries = $model->fetchEntries();
    }

    public function signAction()
    {
        $request = $this->getRequest();
        $form    = $this->_getGuestbookForm();
      
        if ($this->getRequest()->isPost()) {
            
            if ($form->isValid($request->getPost())) {
                
                $model = $this->_getModel();
                $model->save($form->getValues());
               
                return $this->_helper->redirector('index');
            }
        }

        $this->view->form = $form;
    }
    
    protected function _getModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . '/models/GuestBook.php';
            $this->_model = new Model_GuestBook();
        }
        return $this->_model;
    }
  
    protected function _getGuestbookForm()
    {
        require_once APPLICATION_PATH . '/forms/GuestBook.php';
        $form = new Form_GuestBook();
        $form->setAction($this->_helper->url('sign'));
        return $form;
    }
}
